package com.yylending.las.tinker.network;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.YJKD;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by fuqiang on 2017/7/27.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class TinkerPatchModel extends Model {
	private static final String TAG = "TinkerPatchModel";

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJKD.getInstance().getPreferenceManager().getLoginToken();
		return service.getPatch(token, (Params) params);
	}

	@Override
	public void onNext(Object entity) {
		CLog.i(TAG, "onNext: " + entity.toString());
		Result result = cast(entity);
		EventAgent.post(result);
	}

	private interface Service {
		/**
		 * 补丁接口
		 */
		@POST("server/hotfix/getHotfix")
		Observable<Result> getPatch(@Header("token") String token, @Body Params params);
	}

	public static class Params {
		public long timestamp;

		public Params(long time) {
			timestamp = time;
		}
	}

	public static class Result extends BaseResultEntity<ResultBean> {
	}

	public static class ResultBean {
		public long timestamp;//服务端录入修复包的时间戳
		public String url;//补丁包的下载地址
		public String md5;//补丁包的MD5值
		public String versionCode;//版本号
	}
}
